package com.yikome.uaa.utils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.yikome.uaa.domain.Authority;

public class UserAuthoritiesUtils {

	
	public static Set<Authority> collectionUserAuthority(Set<Authority> userMenu) {
		//目录级别
    	Set<Authority> catalogSet = new HashSet<>();
    	Iterator<Authority> catalogIterator = userMenu.iterator();
    	while (catalogIterator.hasNext()) {
    		Authority authority = catalogIterator.next();
			if(authority.getType() == 1 || (authority.getType() == 2 && authority.getParentId() == 0)) {
				catalogSet.add(authority);
				catalogIterator.remove();
			}
		}
    	//目錄-菜單級別整理
    	Iterator<Authority> menuIterator = userMenu.iterator();
    	while (menuIterator.hasNext()) {
    		Authority authority = menuIterator.next();
			if(authority.getType() == 2) {
				putAuthorityToParent(authority, catalogSet);
				menuIterator.remove();
			}
		}
    	
    	//菜單-操作級別整理
    	Iterator<Authority> buttonIterator = userMenu.iterator();
    	while (buttonIterator.hasNext()) {
    		Authority authority = buttonIterator.next();
			if(authority.getType() == 3) {
				putAuthorityToParentChildren(authority, catalogSet);
				buttonIterator.remove();
			}
		}
    	for (Authority authority : catalogSet) {
			if(authority.getChildren() != null && !authority.getChildren().isEmpty()) {
				authority.getChildren().sort((o1 , o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()));
				
				for(Authority authority2 : authority.getChildren()) {
					if(authority2.getChildren() != null && !authority2.getChildren().isEmpty()) {
						authority2.getChildren().sort((o1 , o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()));
					}
				}
			}
		}
    	
    	List<Authority> catalogSetToList = new ArrayList<>(catalogSet);
    	catalogSetToList.sort((o1 , o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()));
    	
    	LinkedHashSet<Authority> result = new LinkedHashSet<>(catalogSetToList);
    	
    	
    	return result;
	}
	
	private static void putAuthorityToParent(Authority authority , Set<Authority> parentAuthorities) {
    	
    	if(authority == null || parentAuthorities == null || parentAuthorities.isEmpty()) return;
    	
    	for (Authority parent : parentAuthorities) {
			if(authority.getParentId().equals(parent.getId())) {
				if(parent.getChildren() == null) {
					List<Authority> childrenAuthorities = new ArrayList<>();
					childrenAuthorities.add(authority);
					parent.setChildren(childrenAuthorities);
				}else {
					parent.getChildren().add(authority);
				}
			}
		}
    }
    
    private static void putAuthorityToParentChildren(Authority authority , Set<Authority> parentAuthorities) {
    	
    	if(authority == null || parentAuthorities == null || parentAuthorities.isEmpty()) return;
    	
    	for (Authority catalog : parentAuthorities) {
    		
    		if(catalog.getType() == 2) {
    			
    			if(authority.getParentId().equals(catalog.getId())) {
					if(catalog.getChildren() == null) {
						List<Authority> childrenAuthorities = new ArrayList<>();
						childrenAuthorities.add(authority);
						catalog.setChildren(childrenAuthorities);
					}else {
						catalog.getChildren().add(authority);
					}
				}
    			
    		}else if(catalog.getType() == 1) {
    			List<Authority> authorities = catalog.getChildren();
    			if(authorities != null && !authorities.isEmpty()) {
    				for (Authority parent : authorities) {
    					if(authority.getParentId().equals(parent.getId())) {
    						if(parent.getChildren() == null) {
    							List<Authority> childrenAuthorities = new ArrayList<>();
    							childrenAuthorities.add(authority);
    							parent.setChildren(childrenAuthorities);
    						}else {
    							parent.getChildren().add(authority);
    						}
    					}
    				}
    			}
    		}
		}
    }
}